﻿using FastMvc.Crud;
using FastMvc.Repositories;
using JQ.SAAS.Entity;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using JQ.SAAS.DTO.SysOrganization;
using JQ.SAAS.IService;
using SqlSugar;
using FastMvc.Models;
using FastMvc.Auth;

namespace TestWebServer.Controllers
{
    /// <summary>
    /// 机构管理
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    [PermissionCheck("Admin.Sys.Organization","机构管理")]
    public class SysOrganizationController : CrudController<sys_organization, SysOrgDto, string, SysOrgListRequestDto, SysOrgCreateUpdateDto, SysOrgCreateUpdateDto>
    {
        private ISysOrgService _sysOrgService;
        public SysOrganizationController(ISysOrgService sysOrgService)
            : base(sysOrgService)
        {
            _sysOrgService = sysOrgService;
        }

        /// <summary>
        /// 获取分配给机构的菜单列表
        /// </summary>
        /// <param name="sOrgId">机构Id</param>
        /// <returns></returns>
        [HttpGet("{sOrgId}/menus")]
        [PermissionCheck("MenusSet", "菜单分配")]
        public async Task<BaseResult<List<SysOrgMenuListDto>>> GetOrgMenus(string sOrgId)
        {
            var data = await _sysOrgService.GetOrgMenuList(sOrgId);

            return await SetResultNotNull(data);
        }

        /// <summary>
        /// 分配机构菜单
        /// </summary>
        /// <param name="sOrgId">机构Id</param>
        /// <param name="sMenus">分配的菜单</param>
        /// <returns></returns>
        [HttpPost("{sOrgId}/menus")]
        [PermissionCheck("MenusSet", "菜单分配")]
        public async Task<BaseResult> SetOrgMenus(string sOrgId, [FromBody] List<SysOrgMenuListDto> sMenus)
        {
            await _sysOrgService.SetOrgMenus(sOrgId, sMenus);

            return BaseResult.Success("设置成功！");
        }
    }
}
